<template>
  <el-card style="margin-bottom: 20px">
    <div slot="header" class="clearfix">
      <span>个人资料</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <!-- <pan-thumb :image="user.avatar" :height="'100px'" :width="'100px'" :hoverable="true">
          <div>欢迎</div>
          {{ user.role }}
        </pan-thumb> -->
        <el-upload
            class="avatar-uploader"
            :action="server"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeUpload"
            ref="upload"
          >
            <img v-if="url" :src="url" class="avatar" :width="'100px'" :height="'100px'"/>
            <img v-else src="@/assets/images/profile1.jpg" class="avatar" :width="'100px'" :height="'100px'"/>
       </el-upload>
      </div>
      <div class="box-center">
        <!-- <div class="user-name text-center">{{ user.name }}</div> -->
        <div class="user-role text-center text-muted">
          <!-- 角色id:{{ user.role | uppercaseFirst }} -->
          <div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <div class="box"><svg-icon icon-class="peoples" />所属角色:</div>
                <div class="pull-right">{{ user.role | uppercaseFirst }}</div>
              </li>
              <li class="list-group-item">
                <div class="box"><svg-icon icon-class="user" />用户名称:</div>
                <div class="pull-right">{{ user.name }}</div>
              </li>
              
              <li class="list-group-item">
                <div class="box"> <svg-icon icon-class="email" />用户邮箱:</div>
                <div class="pull-right">{{ user.email }}</div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import PanThumb from "@/components/PanThumb";
import FileUpload from "@/components/FileUpload/index"
import {updateImage} from '@/api/sys/user/user'
import {mapGetters} from 'vuex'
import {getUser} from "@/api/user"


export default {
  inject: ['reload'],
  components: { PanThumb,FileUpload },
  data() {
    return {
      flag: '',
      server: `${process.env.VUE_APP_BASE_API}/common/api/file/upload`,
    };
  },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          password: ''
        }
      }
    }
  },
  methods: {
    handleAvatarSuccess(response, file, fileList) {
      console.log(response);
      this.imageUrl = `${process.env.VUE_APP_BASE_API}/common/api/file/download?name=${response}`;
      this.flag = response;
      if(!(response === '' && response === undefined)){
        const user = {
          id: this.userId,
          image: response
        }
        this.info.image = response.image
        updateImage(user)
      }
      //进行页面的刷新：
      this.reload()
    },
    submit(){
      upload(url)
    },
    beforeUpload(file) {
      if (file) {
        const suffix = file.name.split(".")[1];
        const size = file.size / 1024 / 1024 < 2;
        if (["png", "jpeg", "jpg"].indexOf(suffix) < 0) {
          this.$message.error("上传图片只支持 png、jpeg、jpg 格式！");
          this.$refs.upload.clearFiles();
          return false;
        }
        if (!size) {
          this.$message.error("上传文件大小不能超过 2MB!");
          return false;
        }
        return file;
      }
    },
  },
  computed:{
    ...mapGetters(['userId','info','url'])
  },
  mounted(){
    getUser(this.userId).then((response)=>{
      //设置用户头像：
      this.flag = response.data.image
      //获取用户相关信息
      this.$store.dispatch('user/getUser',this.userId);
    })
  }
  
};
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
.list-group-striped{
  width: 400px;
  margin: 0 auto;
}
.list-group-item{
    width: 200px;
    height: 25px;
    line-height: 25px;
    list-style-type: none;
    border-bottom: 1px solid gray;
}
.list-group-item .box{
  float: left;
}
.list-group-item .pull-right{
  text-align: left;
  text-indent: 10px;
}
</style>
